/**
 * 
 */
var jiemianChart = null;
var pullChart1 = null;;
var pullChart2 = null;
var pullChart3 = null;

$('document').ready(function(){
	
	initPullSelector();
	
	jiemianChart = echarts.init(document.getElementById('jianmianChart'));
	jiemianChart.setOption(jiemianOption);
	pullChart1 = echarts.init(document.getElementById('pullChart1'));
	pullChart2 = echarts.init(document.getElementById('pullChart2'));
	pullChart3 = echarts.init(document.getElementById('pullChart3'));
});

/**
 * 
 * @param currentTime
 * @returns
 */
function drawPull(currentTime) {
	
	let startTime = new Date(currentTime);
	let pull_1 = $('#pullselector1').val();
	let pull_2 = $('#pullselector2').val();
	let pull_3 = $('#pullselector3').val();
	
	// get control data
	$.ajax({
		url: 'pull/pullControl.do',
		type: 'get',
		data: {
			current: startTime
		}

	}).done(function(data, status, xhr){

		let controlStartTime = null;
		let controlEndTime = null;
		let controlId = null;
		if (data != null) {
			controlStartTime = new Date(data.pullControlStime);
			controlEndTime = new Date(data.pullControlEtime);
			controlId = data.pullControlId;
		}
		// get real data to draw chart

		let pullInterval = setInterval(function(){
			$.ajax({
				url: 'pull/pullData.do',
				type: 'post',
				data: {
					start: startTime,
					interval: HANGYUN.REFRESH_INTERVAL,
					controlId: controlId,
					tran1: pull_1,
					tran2: pull_2,
					tran3: pull_3
				}
			}).done((data, status, xhr)=>{
				debugger;
				if (data != null) {
					optionPull1.series[0].data = data[pull_1];
					pullChart1.setOption(optionPull1);
					
					optionPull2.series[0].data = data[pull_2];
					pullChart2.setOption(optionPull2);
					
					optionPull3.series[0].data = data[pull_3];
					pullChart3.setOption(optionPull3);
				} else {
					clearInterval(pullInterval);
				}
				
				if (startTime > controlEndTime) {
					clearInterval(pullInterval);
				}
				
			});
		}, HANGYUN.REFRESH_INTERVAL*1000);

	});
}

/**
 * 
 */
$('#submit').click(function(){

	var currentTime = new Date();
	console.log(currentTime.getTime());
	if(HANGYUN.DEMO){
		drawPullDemo(currentTime)
	} else {
		drawPull(currentTime);
	}
});

/**
 * 
 * @returns
 */
function initPullSelector(){
	
	$.ajax({
		url: 'pull/pullTrans.do',
        method : "get",
	}).done(function(data, status, xhr){
		for (let i = 0; i < data.length; i++){
			$('#pullselector1').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#pullselector2').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#pullselector3').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
		}
	});
}

/**
 * demo
 * @param {*} currentTime 
 */
function drawPullDemo(currentTime) {
	
	let startTime = new Date(currentTime);
	let endTime = new Date(startTime.getTime()+HANGYUN.DEMO_DURATION*1000*60);
	
	let inter = setInterval(()=>{
		let valueArray1 = [];
		let valueArray2 = [];
		let valueArray3 = [];
		let num1 = 0;
		let num2 = 0;
		let num3 = 0;
		for (let i = 0; i < 100; i++) {
			num1 = Math.random()*(3-2) + 2;
			num2 = Math.random()*(3-2) + 2;
			num3 = Math.random()*(3-2) + 2;
			valueArray1.push(num1);
			valueArray2.push(num2);
			valueArray3.push(num3);
		}
		
		optionPull1.series[0].data = valueArray1;
		pullChart1.setOption(optionPull1);
		
		optionPull2.series[0].data = valueArray2;
		pullChart2.setOption(optionPull2);
		
		optionPull3.series[0].data = valueArray3;
		pullChart3.setOption(optionPull3);
		
		startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
		
		if (startTime > endTime) {
			clearInterval(inter);
		}
		
	}, HANGYUN.REFRESH_INTERVAL*1000);
}
